$(function () {
    $("#jqGrid").jqGrid({
        url: '../trainingdemandmanagement/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },

            { label: '需求年度', name: 'year', index: 'year', width: 80 },
			{ label: '需求部门', name: 'demandDept', index: 'Demand_dept', width: 80 }, 			
			{ label: '需求内容', name: 'internalDemandContent', index: 'Internal_Demand_content', width: 80 },
            { label: '培训形式', name: 'type', index: 'type', width: 80 ,formatter:getDictInfo},
			{ label: '需求时间', name: 'demandTime', index: 'Demand_time', width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'} },
			{ label: '需求人员', name: 'demandPerson', index: 'Demand_person', width: 80 }, 			
			{ label: '预计课时', name: 'expectedTime', index: 'expectedTime', width: 80 },
			{ label: '需求组织部门', name: 'demandOrganizationDepartment', index: 'Demand_Organization_Department', width: 80 }, 			
			{ label: '外部需求内容', name: 'externalDemandContent', index: 'External_Demand_content', width: 80 ,hidden:true},
			{ label: '送培人员', name: 'trainingPerson', index: 'Training_person', width: 80 }, 			
			{ label: '岗位名称', name: 'post', index: 'post', width: 80,formatter:getDictInfo },
			{ label: '岗位id', name: 'postId', index: 'post_id', width: 80,hidden:true },
			{ label: '预计费用', name: 'estimatedCost', index: 'Estimated_cost', width: 80 }, 			
			{ label: '培训单位', name: 'trainingUnit', index: 'training_unit', width: 80 }, 			
			{ label: '培训单位id', name: 'trainingUnitId', index: 'training_unit_id', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    var r = utils.getDictList("post,training");
    vm.dictList = r.post;
    vm.dictList1 = r.training;

 /*   $(".datepicker").datepicker({
        language: "zh-CN",
       // autoclose: true,//选中之后自动隐藏日期选择框
        clearBtn: true,//清除按钮
        todayBtn: true,//今日按钮
        format: "yyyy-mm-dd"//日期格式
    });*/

});



var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            demandOrganizationDepartment: null,
			type:null,
            year: null
        },
		showList: true,
        showtable:false,
        dictList:{},
        dictList1:{},
		title: null,
		trainingDemandManagement: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showtable = true;
			vm.title = "新增";
			vm.trainingDemandManagement = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var type=rowData.type;
            vm.showPage(type);
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            vm.trainingDemandManagement.demandTime=$("#demandTime").val();
            var url = vm.trainingDemandManagement.id == null ? "../trainingdemandmanagement/save" : "../trainingdemandmanagement/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.trainingDemandManagement),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../trainingdemandmanagement/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../trainingdemandmanagement/info/"+id, function(r){
                vm.trainingDemandManagement = r.trainingDemandManagement;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'year': vm.q.year,
                    'type': vm.q.type,
                    'demandOrganizationDepartment': vm.q.demandOrganizationDepartment},
                page:page
            }).trigger("reloadGrid");
		},
        typeChange: function () {
            var type=vm.trainingDemandManagement.type;
           vm.showPage(type);

        },
        showPage: function (type) {
            if(type=='training01'||type=='内部'){
                vm.showtable = true;

            }else if(type=='training02'||type=='外部'){
                vm.showtable = false;
            }else{
                vm.showtable = true;
			}


        },
	}
});


function zfDate(value){
    vm.trainingDemandManagement.demandTime=value;
}
